package com.junyue.control.platform;

import com.alibaba.fastjson.JSONObject;
import com.junyue.control.platform.entity.ConfigBeanValue;
import com.junyue.control.platform.entity.LoginEntity;
import com.junyue.control.platform.entity.ResultMessage;
import com.junyue.control.platform.utils.ReadParamters;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/login"})
public class LoginCheckController {
    @Autowired
    ConfigBeanValue configBeanValue;

    @RequestMapping({"/check"})
    public ResultMessage check(HttpServletRequest request) {
        String params = ReadParamters.getParm(request);
        ResultMessage resultMessage = new ResultMessage();
        if (params != null) {
            LoginEntity loginEntity = (LoginEntity) JSONObject.parseObject(params, LoginEntity.class);
            if ((this.configBeanValue.getLoginName().equalsIgnoreCase(loginEntity.getUsername())) && (this.configBeanValue.getPassword().equalsIgnoreCase(loginEntity.getPassword()))) {
                request.getSession().setAttribute("loginStatus", "loginSuccess");
                resultMessage.setStatus(true);
                resultMessage.setMessage("成功");
            } else {
                resultMessage.setStatus(false);
                resultMessage.setMessage("失败");
            }
        } else {
            resultMessage.setStatus(false);
            resultMessage.setMessage("失败");
        }
        return resultMessage;
    }
}
